package edu.ahpu.propertymanage.util;

import org.springframework.stereotype.Component;

import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Date;

@Component
public class TimeChange {

    public Date addAnyTime(Date date, int mouth) {
        Long time = date.getTime() + 24l * 60 * 60 * 1000 * 30 * mouth;
        return new Date(time);
    }

    public Date addTime(Date date) {
        Long time = date.getTime() + 8l * 60 * 60 * 1000;
        Date date1 = new Date(time);
        return date1;
    }

    public Date deleteTime(Date date) {
        Long time = date.getTime() - 8l * 60 * 60 * 1000;
        Date date1 = new Date(time);
        return date1;
    }

    public Date addAMountTime(Date date) {

        //将date转换为localdate
        Instant instant = date.toInstant();
        ZoneId zoneId = ZoneId.systemDefault();
        // atZone()方法返回在指定时区从此Instant生成的ZonedDateTime。
        LocalDate localDate = instant.atZone(zoneId).toLocalDate();

        //获取月份
        int month = localDate.getMonthValue();
        int year = localDate.getYear();

        Long time;

        if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
            if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 12) {
                time = date.getTime() + 24l * 60 * 60 * 1000 * 31;
            } else if (month == 2) {
                time = date.getTime() + 24l * 60 * 60 * 1000 * 29;
            } else
                time = date.getTime() + 24l * 60 * 60 * 1000 * 30;
        } else {
            if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 12) {
                time = date.getTime() + 24l * 60 * 60 * 1000 * 31;
            } else if (month == 2) {
                time = date.getTime() + 24l * 60 * 60 * 1000 * 28;
            } else
                time = date.getTime() + 24l * 60 * 60 * 1000 * 30;
        }


        date = new Date(time);
        return date;
    }

    public Date addYearTime(Date date) {

        //将date转换为localdate
        Instant instant = date.toInstant();
        ZoneId zoneId = ZoneId.systemDefault();
        // atZone()方法返回在指定时区从此Instant生成的ZonedDateTime。
        LocalDate localDate = instant.atZone(zoneId).toLocalDate();

        int year = localDate.getYear();
        if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
            date = new Date(date.getTime() + 24l * 60 * 60 * 1000 * 366);
        } else
            date = new Date(date.getTime() + 24l * 60 * 60 * 1000 * 365);
        return date;
    }

    public Date addSetMonthTime(Date date, Integer integer) {
        return new Date(date.getTime() + 24l * 60 * 60 * 1000 * 30 * integer);
    }
}
